from django.db import models


# Create your models here.
class Place(models.Model):
    name = models.CharField(verbose_name='地点', max_length=255)

    class Meta:
        verbose_name = '检测地点信息'
        verbose_name_plural = verbose_name

    def __str__(self):
        return f"{self.name}"


class Teacher(models.Model):
    name = models.CharField(verbose_name='教师名', max_length=255)
    teacher_card = models.CharField(verbose_name='一卡通', max_length=10)

    class Meta:
        verbose_name = '教工信息'
        verbose_name_plural = verbose_name

    def __str__(self):
        return f"【教工】{self.name}"


class Trelation(models.Model):
    name = models.CharField(verbose_name='联系人姓名', max_length=255)
    teacher = models.ForeignKey(Teacher, verbose_name='联系教师', on_delete=models.SET_NULL, null=True, blank=True)

    class Meta:
        verbose_name = '教工关系人'
        verbose_name_plural = verbose_name

    def __str__(self):
        return f"【{self.teacher.name}】{self.name}"


class TAct(models.Model):
    name = models.CharField(verbose_name='教工活动', max_length=255)
    start_time = models.DateTimeField(verbose_name='开始时间')
    status = models.IntegerField(verbose_name='状态', choices=((1, '进行中'), (2, '结束')), default=1)
    description = models.CharField(verbose_name='备注', max_length=255, null=True, blank=True)

    class Meta:
        verbose_name = '教工活动'
        verbose_name_plural = verbose_name

    def __str__(self):
        return f"【{self.get_status_display()}】{self.name}"


class TALog(models.Model):
    person = models.ForeignKey(Trelation, verbose_name='活动对象', on_delete=models.SET_NULL, null=True, blank=True)
    activity = models.ForeignKey(TAct, verbose_name='教工活动', on_delete=models.SET_NULL, null=True, blank=True)
    place = models.ForeignKey(Place, verbose_name='检测地址', on_delete=models.SET_NULL, null=True, blank=True)
    img_url = models.CharField(verbose_name='图片链接', null=True, blank=True, max_length=255)
    create_time = models.DateTimeField(verbose_name='创建时间', auto_created=True, null=True, blank=True)

    class Meta:
        verbose_name = '教工参与活动记录'
        verbose_name_plural = verbose_name

    def __str__(self):
        return f"【{self.activity}】{self.person.teacher.name}--{self.person.name} ({self.create_time})"


class TNoLog(models.Model):
    act = models.ForeignKey(TAct, verbose_name='活动', on_delete=models.SET_NULL, null=True, blank=True)
    # act_name = models.CharField(max_length=100, verbose_name='活动名')
    teacher = models.ForeignKey(Teacher, verbose_name='联系教工', on_delete=models.SET_NULL, null=True, blank=True)
    person = models.ForeignKey(Trelation, verbose_name='姓名', on_delete=models.SET_NULL, null=True, blank=True)

    class Meta:
        managed = False
        db_table = "Tno_log"
        verbose_name = '未完成教工'
        verbose_name_plural = verbose_name
